package com.amoyt.project.controller.visitor;


import com.amoyt.project.domain.dto.insert.FeedbackAddDto;
import com.amoyt.project.domain.dto.query.FeedbackPageQuery;
import com.amoyt.project.domain.pojo.Feedback;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.exception.BusinessException;
import com.amoyt.project.exception.enums.BusinessExceptionEnum;
import com.amoyt.project.service.visitor.FeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 意见反馈表（评论表） 前端控制器
 * </p>
 *
 * @author AmoyT
 * @since 2025-08-21
 */
@RestController
@RequestMapping("/visitor/feedback")
@Tag(name = "意见反馈接口",description = "意见反馈表接口")
@Slf4j
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 个人意见反馈列表
     * @param query
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "个人意见反馈的列表",method = "POST")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasRole('user')")
    public Result list(@RequestBody FeedbackPageQuery query) {
        log.info("个人意见反馈列表 query={}",query);
        if (query == null || query.getPage() == null || query.getPageSize() == null){
            Result.error("参数错误");
        }

        return feedbackService.list(query);
    }

    /**
     * 删除个人意见反馈
     * @param backIds
     * @return
     */
    @DeleteMapping("/delete/{backIds}")
    @Operation(summary = "删除个人意见反馈",method = "DELETE")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasRole('user')")
    public Result delete(@PathVariable List<Long> backIds) {
        if (backIds == null || backIds.size() == 0){
            return Result.error("参数错误");
        }

        return feedbackService.delete(backIds);
    }

    /**
     * 个人意见反馈详情
     * @param backId
     * @return
     */
    @GetMapping("/detail/{backId}")
    @Operation(summary = "个人意见反馈详情",method = "GET")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasRole('user')")
    public Result detail(@PathVariable Long backId) {
        if (backId == null){
            return Result.error("参数错误");
        }

        return feedbackService.detail(backId);
    }

    /**
     * 做一次反馈，或者投诉
     * @param feedbackAddDto
     * @return
     */
    @PostMapping("/feed-back")
    @Operation(summary = "do 反馈或者投诉",method = "POST")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasRole('user')")
    public Result doFeedBack(@RequestBody FeedbackAddDto feedbackAddDto) {
        if (feedbackAddDto == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return feedbackService.doFeedBack(feedbackAddDto);
    }
}

